using B.S.XZYData.Api.Wrter.Application.Command.WorkOrder;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Wrter.Controllers
{
    /// <summary>
    /// 工单写操作控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class WorkOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public WorkOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 保存工单（新增或更新）
        /// </summary>
        /// <param name="command">工单保存命令</param>
        /// <returns></returns>
        [HttpPost("save")]
        public async Task<ApiResult<int>> SaveWorkOrder([FromBody] SaveWorkOrderCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="id">工单ID</param>
        /// <param name="operateBy">操作人</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<bool>> DeleteWorkOrder(int id, [FromQuery] string operateBy = "System")
        {
            var command = new DeleteWorkOrderCommand
            {
                Id = id,
                OperateBy = operateBy
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 批量删除工单
        /// </summary>
        /// <param name="ids">工单ID列表</param>
        /// <param name="operateBy">操作人</param>
        /// <returns></returns>
        [HttpDelete("batch")]
        public async Task<ApiResult<bool>> BatchDeleteWorkOrder([FromBody] List<int> ids, [FromQuery] string operateBy = "System")
        {
            var command = new BatchDeleteWorkOrderCommand
            {
                Ids = ids,
                OperateBy = operateBy
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 开始工单
        /// </summary>
        /// <param name="id">工单ID</param>
        /// <param name="operateBy">操作人</param>
        /// <returns></returns>
        [HttpPut("{id}/start")]
        public async Task<ApiResult<bool>> StartWorkOrder(int id, [FromQuery] string operateBy = "System")
        {
            var command = new StartWorkOrderCommand
            {
                Id = id,
                OperateBy = operateBy
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 结束工单
        /// </summary>
        /// <param name="id">工单ID</param>
        /// <param name="operateBy">操作人</param>
        /// <returns></returns>
        [HttpPut("{id}/finish")]
        public async Task<ApiResult<bool>> FinishWorkOrder(int id, [FromQuery] string operateBy = "System")
        {
            var command = new FinishWorkOrderCommand
            {
                Id = id,
                OperateBy = operateBy
            };

            return await _mediator.Send(command);
        }
    }
}
